module("UI.Windows.WebViewWindow", package.seeall)

local NetMgr = require("NetWork").NetMgr
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
local UIUtility = require("UI.Utility")
local atools = require("StringTool").StringTool
local UIWindow = require("UI.UIWindow").UIWindow
local GameObject = UnityEngine.GameObject
WebViewWindow = UIWindow:new()
WebViewWindow.name = "WebViewWindow"


function WebViewWindow:Init()
    local t = self.Container.transform

    self.CanvasWebViewPrefab = UIUtility.GetChildTransform("CanvasWebViewPrefab", t, true)
    --self.CanvasWebViewPrefab = UIUtility.GetChildTransform("CanvasWebViewPrefab", t, true)
    self.data_info = UIUtility.GetChildTransform("data_info", t, true)
    self.data1 = self.data_info.transform:GetChild(0)
    self.close_btn = UIUtility.GetChildTransform("close_btn", t, true)

    --1 客服  3瓦力  4bbin 5we
    self.gamePlatformId = 0
    --1 视讯 2捕鱼 3棋牌 4 电子 5体育 6 彩票
    self.gameType = 0

    self:BindEvents()
end

function WebViewWindow:OnClose()

end

--https://9med42zz.ddwkx.cn/video/?uid=178223056&game=80&token=MTc4MjIzMDU2XzE3NDk4ODI1NzM3Mjg6WnhNaU1ZYWIzWVlqMlM3eg
--如果gamePlatformId 1  则无需判断，直接给位置 并显示
--如果gamePlatformId 2  则判断url   gameTpe 为1 为竖屏
function WebViewWindow:urlChanged()
    local tempUrlArr = atools:Split(self.data1.gameObject.name, "serverId")
    if self.gamePlatformId == 1 then
        self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-20")
        self.close_btn.gameObject:SetActive(true)
    elseif self.gamePlatformId == 3 then
        if self.gameType == 1 then
            --通过?id= 判断是否在视讯游戏里
            tempUrlArr = atools:Split(self.data1.gameObject.name, "?id=")
            self.close_btn.gameObject:SetActive(#tempUrlArr == 1)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-20")
        else
             
            self.close_btn.gameObject:SetActive(true)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        end
       
    elseif self.gamePlatformId == 4 then
        if self.gameType == 1 then
            tempUrlArr = atools:Split(self.data1.gameObject.name, "&HALLID=")
            self.close_btn.gameObject:SetActive(#tempUrlArr > 1)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        else
          
            self.close_btn.gameObject:SetActive(true)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        end
        
    elseif self.gamePlatformId == 5 then
        if self.gameType == 1 then
            tempUrlArr = atools:Split(self.data1.gameObject.name, "&HALLID=")
            self.close_btn.gameObject:SetActive(#tempUrlArr > 1)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        else
          
            self.close_btn.gameObject:SetActive(true)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        end
    elseif self.gamePlatformId == 6 then
        if self.gameType == 1 then
            tempUrlArr = atools:Split(self.data1.gameObject.name, "&HALLID=")
            self.close_btn.gameObject:SetActive(#tempUrlArr > 1)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        else
          
            self.close_btn.gameObject:SetActive(true)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        end
    else
   
        if self.gameType == 1 then
            tempUrlArr = atools:Split(self.data1.gameObject.name, "&HALLID=")
            self.close_btn.gameObject:SetActive(#tempUrlArr > 1)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        else
          
            self.close_btn.gameObject:SetActive(true)
            --位置设置到 左上
            self.CanvasWebViewPrefab.gameObject:SendMessage("SetBtnPos", "0_1_0_1_0_1_0_0_0_150_-40")
        end
        
    end
end

function WebViewWindow:CloseWin()
    self:SetAudioMuted(true)
    self.CanvasWebViewPrefab.gameObject:SetActive(false)
    self:Close()
    self:Destroy()
    require("UI.Windows.NewGameLoobyWin").NewGameLoobyWin:xiaFenUpdate()

    local setSound = UnityEngine.PlayerPrefs.GetString("SetSound")
    local SoundModule = require("Module.SoundModule").SoundModule
    if setSound == "1" then
        SoundModule:SetAllBgVolume(0.2)
    else
        SoundModule:SetAllBgVolume(0)
    end
end

function WebViewWindow:BindEvents()

end

function WebViewWindow:SetUrl(url)
    if self.CanvasWebViewPrefab then
        self.CanvasWebViewPrefab.gameObject:SetActive(true)
        self.CanvasWebViewPrefab.gameObject:SendMessage("LoadUrl", url)
    else

    end
end

function WebViewWindow:SetAudioMuted(muted)
    if self.CanvasWebViewPrefab then
        self.CanvasWebViewPrefab.gameObject:SendMessage("SetAudioMuted", muted)
    end
end

function WebViewWindow:OnShow()
     self.close_btn.gameObject:SetActive(false)
end
